package com.howie.hmvc.paramparse;

import java.lang.annotation.Annotation;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.howie.hmvc.annotations.Valid;
import com.howie.hmvc.exception.ArguErrorException;

public class ParamBooleans extends AbsParamParse{
	
private boolean required;
	
	@Override
	public void init(Annotation[] an) throws Exception {
		Valid valid = getValid(an);
		if(valid != null){
			required = valid.required();
		}
	}
	
	@Override
	public Object getParamter(HttpServletRequest request,HttpServletResponse response)throws Exception{
		String[] values = request.getParameterValues(name);
		if(values == null){
			if(required){
				throw new ArguErrorException(name,"required",null);
			}
		}else{
			Boolean[] vals = new Boolean[values.length];
			for(int i=0;i<values.length;i++){
				if(required && values[i] == null){
					throw new ArguErrorException(name,"required",null);
				}
				vals[i] = Boolean.parseBoolean(values[i]);
			}
			return vals;
		}
		return null;
	}

}
